﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC_DaLatServices.Models;
using System.ComponentModel.DataAnnotations;

namespace MVC_DaLatServices.Areas.Administrator.Controllers
{
    public class ArticleController : Controller
    {
        private DaLatServiceEntities db = new DaLatServiceEntities();

        //
        // GET: /Administrator/Article/

        public ActionResult Index()
        {
            var articles = db.Articles.Include(a => a.Category).Include(a => a.ArticleFrontPage);
            return View(articles.ToList());
        }

        //
        // GET: /Administrator/Article/Details/5

        public ActionResult Details(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            return View(article);
        }

        //
        // GET: /Administrator/Article/Create

        public ActionResult Create()
        {
            //CategoryID sẽ được gọi trong view cho Dropdown List
            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name");
            ViewBag.ArticleID = new SelectList(db.ArticleFrontPages, "ArticleID", "ArticleID");
            return View();
        }

        //
        // POST: /Administrator/Article/Create

        [HttpPost]
        [ValidateInput(false)]
        //[CustomAuthorize
        [ValidateAntiForgeryToken]
        public ActionResult Create(Article article)
        {
            var rs = article;
            if (ModelState.IsValid)
            {
                db.Articles.Add(article);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name");
            ViewBag.ArticleID = new SelectList(db.ArticleFrontPages, "ArticleID", "ArticleID", article.ArticleID);
            return View(article);
        }

        //
        // GET: /Administrator/Article/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name", article.CategoryID);
            ViewBag.ArticleID = new SelectList(db.ArticleFrontPages, "ArticleID", "ArticleID", article.ArticleID);
            return View(article);
        }

        //
        // POST: /Administrator/Article/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [Display(Name = "Cateogry")]
        public ActionResult Edit(Article article)
        {
            article.CategoryID = 1;
            if (!ModelState.IsValid)
            {
                db.Entry(article).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name", article.CategoryID);
            ViewBag.ArticleID = new SelectList(db.ArticleFrontPages, "ArticleID", "ArticleID", article.ArticleID);
            return View(article);
        }

        //
        // GET: /Administrator/Article/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            return View(article);
        }

        //
        // POST: /Administrator/Article/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Article article = db.Articles.Find(id);
            db.Articles.Remove(article);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}